package frame;

import pojo.UserDO;
import service.UserService;
import service.impl.UserServiceImpl;
import util.Util;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Menu extends JPanel implements ActionListener {
    private Atm frame;
    private UserService userService = new UserServiceImpl();
    private Icon btnSave = Util.getIcon("/images/buttonSave.jpg");
    private Icon btnGet = Util.getIcon("/images/buttonGet.jpg");
    private Icon btnFind = Util.getIcon("/images/buttonFind.jpg");
    private Icon btnOff = Util.getIcon("/images/buttonOff.jpg");
    private Icon btnModify = Util.getIcon("/images/buttonModify.jpg");
    private JButton imageBtn1 = new JButton(btnSave);
    private JButton imageBtn2 = new JButton(btnGet);
    private JButton imageBtn3 = new JButton(btnFind);
    private JButton imageBtn4 = new JButton(btnOff);
    private JButton btn5 = new JButton("转账");
    private JButton imageBtn6 = new JButton(btnModify);


    public Menu(Atm frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);
        JLabel menu = new JLabel("菜单");
        menu.setBounds(50, 50, 100, 50);
        this.add(menu);


        imageBtn1.setBounds(300, 400, 140, 50);
        this.add(imageBtn1);
        imageBtn1.addActionListener(this);
        imageBtn2.setBounds(600, 400, 140, 50);
        this.add(imageBtn2);
        imageBtn2.addActionListener(this);
        imageBtn3.setBounds(300, 500, 140, 50);
        this.add(imageBtn3);
        imageBtn3.addActionListener(this);
        imageBtn4.setBounds(600, 600, 140, 50);
        this.add(imageBtn4);
        imageBtn4.addActionListener(this);

        btn5.setBounds(600, 500, 140, 50);
        this.add(btn5);
        btn5.addActionListener(this);
        imageBtn6.setBounds(300, 600, 140, 50);
        this.add(imageBtn6);
        imageBtn6.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == imageBtn4) {
            frame.switchPanel("login");
        } else if (e.getSource() == imageBtn1) {
            frame.switchPanel("saveMoney");
        } else if (e.getSource() == imageBtn2) {
            frame.switchPanel("getMoney");
        } else if (e.getSource() == imageBtn3) {

            UserDO user = userService.getByUsernameAndPwd(frame.getUser().getAccount(), frame.getUser().getPsw());
            frame.getFindMoney().getAccountText().setText(user.getBalance() + "");
            frame.switchPanel("findMoney");
        } else if (e.getSource() == btn5) {
            frame.switchPanel("transferMoney");
        } else if (e.getSource() == imageBtn6) {
            frame.switchPanel("modifyMoney");
        }
    }
}
